/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.google.code.tretris.rendering;

import javax.microedition.khronos.opengles.GL10;

/**
 * This abstract class (short for Model View Object) handles positioning,
 * scaling, rotation and setting a single color when rendering to Open GL. This
 * class is not thread safe.
 * 
 * @author Johan Maasing
 * 
 */
public abstract class MVObject implements Renderable {

	protected float x = 0;
	protected float y = 0;
	protected float z = 0;
	protected float rx = 0;
	protected float ry = 0;
	protected float rz = 0;
	protected float sx = 1;
	protected float sy = 1;
	protected float sz = 1;
	protected float red = 0.5f;
	protected float green = 0.5f;
	protected float blue = 0.5f;

	public MVObject() {
		super();
	}

	public void setPosition(final float x, final float y, final float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public void setRotationX(final float rx) {
		this.rx = rx;
	}

	public void setRotationY(final float ry) {
		this.ry = ry;
	}

	public void setRotationZ(final float rz) {
		this.rz = rz;
	}

	public void setColor(final float red, final float green, final float blue) {
		this.green = green;
		this.red = red;
		this.blue = blue;
	}

	public void setScale(final float sx, final float sy, final float sz) {
		this.sx = sx;
		this.sy = sy;
		this.sz = sz;
	}

	public void render(final GL10 gl) {
		gl.glPushMatrix();

		gl.glTranslatef(x, y, z);
		gl.glRotatef(rx, 1, 0, 0);
		gl.glRotatef(ry, 0, 1, 0);
		gl.glRotatef(rz, 0, 0, 1);
		gl.glScalef(sx, sy, sz);

		gl.glColor4f(red, green, blue, 1.0f);

		renderShape(gl);

		gl.glPopMatrix();
	}

	/**
	 * Subclasses should render the visible shape in this method. The modelview
	 * matrix is populated with the rotation, translation and scaling before
	 * calling this method. A call to glColor has also been made but _not_ to
	 * <code>gl.glEnable(GL10.GL_COLOR_MATERIAL);</code>
	 * 
	 * @param gl
	 */
	protected abstract void renderShape(final GL10 gl);
}